package com.sjsu.cloudmanager.client.ui;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.*;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.grid.*;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;
import com.sjsu.cloudmanager.client.CloudManager;
import com.sjsu.cloudmanager.client.LabManagerEvents;
import com.sjsu.cloudmanager.client.service.CloudLabServiceAsync;
import com.sjsu.cloudmanager.client.ui.model.CloudLabBean;
import org.aspectj.weaver.patterns.FormalBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * Grid panel for showing lab data
 * User: ckempaiah
 * Date: 9/23/11
 * Time: 1:01 AM
 * To change this template use File | Settings | File Templates.
 */
public class LabGrid extends LayoutContainer {

    private ListStore<ModelData> gridStore;
    private ContentPanel contentPanel;
    private Grid grid;
    private LabFormPanel labFormPanel;

    @Override
    protected void onRender(Element parent, int index) {
        super.onRender(parent, index);
        setBorders(false);

        ContentPanel cp = new ContentPanel();
        //setStyleAttribute("margin", "10px");
        contentPanel = cp;
        cp.setFrame(true);
        cp.setHeight(650);
        cp.setHeaderVisible(false);
        cp.setLayout(new RowLayout(Style.Orientation.HORIZONTAL));
        cp.setBorders(false);
        List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
        ColumnConfig cc1 = new ColumnConfig("labName", "Lab Name", 80);
        ColumnConfig cc2 = new ColumnConfig("labStatus", "Lab Status", 80);
        CheckBoxSelectionModel<ModelData> checkBoxSelectionModel = new CheckBoxSelectionModel<ModelData>();
        configs.add(checkBoxSelectionModel.getColumn());
        configs.add(cc1);
        configs.add(cc2);
        BaseListLoader<BaseListLoadResult<ModelData>> loader = getLoader();
        ListStore<ModelData> store = new ListStore<ModelData>(loader);
        store.setMonitorChanges(true);
        //store.add(getLabInfo());
        gridStore = store;
        ColumnModel cm = new ColumnModel(configs);

        Grid<ModelData> gd = new Grid<ModelData>(store, cm);
        gd.getView().setEmptyText("Empty");
        gd.setBorders(false);
        gd.setAutoExpandColumn("labName");
        gd.setSelectionModel(checkBoxSelectionModel);
        //grid.addPlugin(checkBoxSelectionModel);
        ToolBar toolBar = new ToolBar();
        toolBar.setBorders(false);

        toolBar.setAlignment(Style.HorizontalAlignment.LEFT);
        toolBar.add(new Button("Add", new SelectionListener<ButtonEvent>() {
            public void componentSelected(ButtonEvent ce) {

               labFormPanel.getFormBinding().clear();
               labFormPanel.getFormBinding().bind(new CloudLabBean());
            }
        }));
        toolBar.add(new Button("Edit", new SelectionListener<ButtonEvent>() {
            public void componentSelected(ButtonEvent ce) {
                if (grid.getSelectionModel().getSelectedItem() != null) {
                    CloudLabBean labBean = (CloudLabBean)grid.getSelectionModel().getSelectedItem();
                    CloudLabBean copy = new CloudLabBean(labBean.getLabId(), labBean.getLabName(), labBean.getLabStatus());
                    labFormPanel.getFormBinding().bind(copy);

                }
            }
        }));
        cp.setTopComponent(toolBar);
        cp.add(gd, new RowData(.5, 1));
        cp.setStyleAttribute("padding", "0");
        this.grid = gd;

        LabFormPanel fp = new LabFormPanel();
        labFormPanel = fp;

        cp.add(fp, new RowData(.5, 1));


        add(cp);

//        grid.getSelectionModel().addListener(Events.SelectionChange, new Listener<SelectionChangedEvent<CloudLabBean>>() {
//            public void handleEvent(SelectionChangedEvent<CloudLabBean> be) {
//                if (be.getSelection().size() > 0) {
//                    formPanel.getFormBinding().bind((ModelData) be.getSelection().get(0));
//                } else {
//                    formPanel.getFormBinding().unbind();
//                }
//            }
//        });
        grid.addListener(Events.Render, new Listener<GridEvent<ModelData>>() {
            @Override
            public void handleEvent(GridEvent<ModelData> modelDataGridEvent) {
                gridStore.getLoader().load();

            }
        });

        ToolbarAction.getInstance().addListener(LabManagerEvents.SaveLab, new Listener<BaseEvent>() {
            @Override
            public void handleEvent(BaseEvent be) {
                gridStore.getLoader().load();
            }
        });
        //setLayout(new FitLayout());
    }


    public ListStore<ModelData> getGridStore() {
        return gridStore;
    }


    private BaseListLoader<BaseListLoadResult<ModelData>> getLoader() {
        final CloudLabServiceAsync service = (CloudLabServiceAsync) Registry.get(CloudManager.CLOUD_LAB_SERVICE);

        RpcProxy<java.util.List<com.sjsu.cloudmanager.client.ui.model.CloudLabBean>> proxy = new RpcProxy<java.util.List<com.sjsu.cloudmanager.client.ui.model.CloudLabBean>>() {
            @Override
            public void load(Object loadConfig,
                             AsyncCallback<java.util.List<com.sjsu.cloudmanager.client.ui.model.CloudLabBean>> callback) {
                service.getCloudLabBeanList(callback);
            }
        };

        final BaseListLoader<BaseListLoadResult<ModelData>> loader = new BaseListLoader<BaseListLoadResult<ModelData>>(proxy);
        return loader;
    }

    private List<CloudLabBean> getLabInfo() {
        List<CloudLabBean> labBeanList = new ArrayList<CloudLabBean>();
        CloudLabBean cloudLabBean = new CloudLabBean();
        cloudLabBean.setLabName("lab1");
        cloudLabBean.setLabStatus("saved");
        labBeanList.add(cloudLabBean);

        cloudLabBean = new CloudLabBean();
        cloudLabBean.setLabName("lab2");
        cloudLabBean.setLabStatus("saved");
        labBeanList.add(cloudLabBean);

        cloudLabBean = new CloudLabBean();
        cloudLabBean.setLabName("lab3");
        cloudLabBean.setLabStatus("saved");
        labBeanList.add(cloudLabBean);

        cloudLabBean = new CloudLabBean();
        cloudLabBean.setLabName("lab4");
        cloudLabBean.setLabStatus("saved");
        labBeanList.add(cloudLabBean);

        cloudLabBean = new CloudLabBean();
        cloudLabBean.setLabName("lab5");
        cloudLabBean.setLabStatus("saved");
        labBeanList.add(cloudLabBean);

        cloudLabBean = new CloudLabBean();
        cloudLabBean.setLabName("lab6");
        cloudLabBean.setLabStatus("saved");
        labBeanList.add(cloudLabBean);

        return labBeanList;

    }

    public void addFormToContentPanel(Widget widget) {
        contentPanel.add(widget, new RowData(.5, 1));
    }
}
